import Vue from "vue";
import VueRouter from "vue-router";
import store from "@/store";
import Cookie from "js-cookie";
let directPath = localStorage.getItem("role") === "1" ? "/home/patientInfo":"/home/instituteInfo"
console.log("user role : ")
console.log(localStorage.getItem("role"))
console.log(localStorage.getItem("role") === "1")
console.log(directPath)

let patient = [
  {
    path: "/",
    name: "login",
    component: () => import("@/views/Login"),
    meta: {
      title: "登录",
      index: 1,
    },
  },
  {
    path: "/register",
    name: "Register",
    component: () => import("@/views/Register"),
    meta: {
      title: "新管理员注册",
    },
  },
  {
    path: "/home",
    name: "home",
    // redirect: "/home/databoard",
    redirect: directPath,
    component: () => import("@/views/Home"),
    meta: {
      title: "首页",
      index: 3,
    },

    children: [
      {
        path: '/home/patientInfo',
        name: 'PatientInfo',
        icon: 'iconfont icon-bingli',
        component: () => import("@/views/PatientInfo"),
        meta: {
          title: "患者信息",
          showTwo: false,
        }
      },
      {
        path: "/home/relation",
        name: "Relation",
        icon: "iconfont icon-bingli",
        component: () => import("@/views/Relation"),
        meta: {
          title: "亲属关系查看",
          showTwo: false,
        },
      },
      {
        path: "/home/medicalRecord",
        name: "MedicalRecord",
        icon: "iconfont icon-bingli",
        component: () => import("@/views/MedicalRecord"),
        meta: {
          title: "病历查看",
          showTwo: false,
        },
      },

      {
        path: "/home/organization",
        name: "organization",
        icon: "iconfont icon-jigou",
        component: () => import("@/views/Organization"),
        meta: {
          title: "就医申请",
          
        },
      },
      {
        path: "/home/deptPatient",
        name: "deptPatient",
        icon: "iconfont icon-jigou",
        hidden: true,
        component: () => import("@/views/DeptPatient"),
        meta: {
          title: "机构医生查看",
          showTwo: true,
        },
      },
      {
        path: "/home/PersonMedicalInfo",
        name: "PersonMedicalInfo",
        icon: "iconfont icon-bingli",
        component: () => import("@/views/PersonMedicalInfo"),
        meta: {
          title: "患者病历信息",
          showTwo: true,

        },
      }, 
    ],
  },
];

let institution = [
  {
    path: "/",
    name: "login",
    component: () => import("@/views/Login"),
    meta: {
      title: "登录",
      index: 1,
    },
  },
  {
    path: "/register",
    name: "Register",
    component: () => import("@/views/Register"),
    meta: {
      title: "新管理员注册",
    },
  },
  {
    path: "/home",
    name: directPath,
    // redirect: "/home/databoard",
    redirect: "/home/instituteInfo",
    component: () => import("@/views/Home"),
    meta: {
      title: "首页",
      index: 2,
    },
    children: [
      // {
      //   path: "/home/orgmanagement",
      //   name: "orgmanagement",
      //   icon: "iconfont icon-jigou",
      //   redirect: "/home/orgforadmin",
      //   component: () => import("@/views/OrganizationInfo"),
      //   meta: {
      //     title: "机构管理",
      //   },
      // },
      {
        path: "/home/instituteInfo",
        name: "instituteInfo",
        icon: "iconfont icon-bingli",
        component: () => import("@/views/InstituteInfo"),
        meta: {
          title: "机构用户信息",
        },
      },
      {
        path: "/home/orgforadmin",
        props: true,
        name: "orgforadmin",
        icon: "iconfont icon-jigou",
        component: () => import("@/views/OrgforAdmin"),
        meta: {
          title: "科室查看",
        },
      },
      {
        path: "/home/Department",
        name: "Department",
        icon: "iconfont icon-jigou",
        component: () => import("@/views/Department"),
        meta: {
          title: "科室医生查看（机构）",
        },
      },
      {
        path: "/home/CurPatientInfo",
        name: "CurPatientInfo",
        icon: "iconfont icon-bingli",
        component: () => import("@/views/CurPatientInfo"),
        meta: {
          title: "当前就诊患者 （机构）",
        },
      },
      {
        path: "/home/PersonMedicalInfo",
        name: "PersonMedicalInfo",
        icon: "iconfont icon-bingli",
        component: () => import("@/views/PersonMedicalInfo"),
        meta: {
          title: "患者病历信息",
          showTwo: true,

        },
      }, 

      // {
      //   path: "/home/Doc",
      //   name: "Doc",
      //   icon: "iconfont icon-bingli",
      //   component: () => import("@/views/Doc"),
      //   meta: {
      //     title: "科室医生查看 （机构）",
      //   },
      // },
    ],
  },
];

// let admin = [
//   {
//     path: "/",
//     name: "login",
//     component: () => import("@/views/Login"),
//     meta: {
//       title: "登录",
//       index: 1,
//     },
//   },
//   {
//     path: "/register",
//     name: "Register",
//     component: () => import("@/views/Register"),
//     meta: {
//       title: "新管理员注册",
//     },
//   },
//   {
//     path: "/home",
//     name: "home",
//     // redirect: "/home/databoard",
//     redirect: directPath,
//     component: () => import("@/views/Home"),
//     meta: {
//       title: "首页",
//       index: 3,
//     },

//     children: [
//       {
//         path: "/home/relation",
//         name: "Relation",
//         icon: "iconfont icon-bingli",
//         component: () => import("@/views/Relation"),
//         meta: {
//           title: "亲属关系查看",
//         },
//       },
//       {
//         path: "/home/medicalRecord",
//         name: "MedicalRecord",
//         icon: "iconfont icon-bingli",
//         component: () => import("@/views/MedicalRecord"),
//         meta: {
//           title: "病历查看",
//         },
//       },

//       {
//         path: "/home/organization",
//         name: "organization",
//         icon: "iconfont icon-jigou",
//         component: () => import("@/views/Organization"),
//         meta: {
//           title: "就医申请",
//         },
//       },
//       {
//         path: "/home/orgforadmin",
//         props: true,
//         name: "orgforadmin",
//         icon: "iconfont icon-jigou",
//         component: () => import("@/views/OrgforAdmin"),
//         meta: {
//           title: "科室查看",
//         },
//       },
//       {
//         path: "/home/instituteInfo",
//         name: "instituteInfo",
//         icon: "iconfont icon-bingli",
//         component: () => import("@/views/InstituteInfo"),
//         meta: {
//           title: "123",
//         },
//       },
//       {
//         path: "/home/Department",
//         name: "Department",
//         icon: "iconfont icon-jigou",
//         component: () => import("@/views/Department"),
//         meta: {
//           title: "科室医生查看",
//         },
//       },
//       {
//         path: "/home/Doc",
//         name: "Doc",
//         icon: "iconfont icon-bingli",
//         component: () => import("@/views/Doc"),
//         meta: {
//           title: "科室医生查看",
//         },
//       },
//       {
//         path: "/home/CurPatientInfo",
//         name: "CurPatientInfo",
//         icon: "iconfont icon-bingli",
//         component: () => import("@/views/CurPatientInfo"),
//         meta: {
//           title: "当前就诊患者",
//         },
//       },
//       {
//         path: "/home/PersonMedicalInfo",
//         name: "PersonMedicalInfo",
//         icon: "iconfont icon-bingli",
//         component: () => import("@/views/PersonMedicalInfo"),
//         meta: {
//           title: "患者病历信息",
//         },
//       },
//       {
//         path: "/home/deptPatient",
//         name: "deptPatient",
//         icon: "iconfont icon-jigou",
//         hidden: true,
//         component: () => import("@/views/DeptPatient"),
//         meta: {
//           title: "机构医生查看",
//         },
//       },
      
//     ],
//   },
// ];

// 使用路由


// const routes = [
//   {
//     path: "/",
//     name: "login",
//     component: () => import("@/views/Login"),
//     meta: {
//       title: "登录",
//       index: 1,
//     },
//   },
//   {
//     path: "/register",
//     name: "Register",
//     component: () => import("@/views/Register"),
//     meta: {
//       title: "新管理员注册",
//     },
//   },
//   {
//     path: "/home",
//     name: "home",
//     // redirect: "/home/databoard",
//     redirect: "/home/Doc",
//     component: () => import("@/views/Home"),
//     meta: {
//       title: "首页",
//       index: 3,
//     },

//     children: [
//       {
//         path: "/home/relation",
//         name: "Relation",
//         icon: "iconfont icon-bingli",
//         component: () => import("@/views/Relation"),
//         meta: {
//           title: "亲属关系查看",
//         },
//       },
//       {
//         path: "/home/medicalRecord",
//         name: "MedicalRecord",
//         icon: "iconfont icon-bingli",
//         component: () => import("@/views/MedicalRecord"),
//         meta: {
//           title: "病历查看",
//         },
//       },

//       {
//         path: "/home/organization",
//         name: "organization",
//         icon: "iconfont icon-jigou",
//         component: () => import("@/views/Organization"),
//         meta: {
//           title: "就医申请",
//         },
//       },
//       {
//         path: "/home/orgforadmin",
//         props: true,
//         name: "orgforadmin",
//         icon: "iconfont icon-jigou",
//         component: () => import("@/views/OrgforAdmin"),
//         meta: {
//           title: "科室查看 （管理员）",
//         },
//       },
//       {
//         path: "/home/Department",
//         name: "Department",
//         icon: "iconfont icon-jigou",
//         component: () => import("@/views/Department"),
//         meta: {
//           title: "科室医生查看（机构）",
//         },
//       },
//       // {
//       //   path: "/home/deptPatient",
//       //   name: "deptPatient",
//       //   icon: "iconfont icon-jigou",
//       //   component: () => import("@/views/DeptPatient"),
//       //   meta: {
//       //     title: "机构医生查看",
//       //   },
//       // },
//       // {
//       //   path: "/home/Department",
//       //   name: "Department",
//       //   icon: "iconfont icon-jigou",
//       //   component: () => import("@/views/Department"),
//       //   meta: {
//       //     title: "查看科室",
//       //   },
//       // },
//       {
//         path: "/home/Doc",
//         name: "Doc",
//         icon: "iconfont icon-bingli",
//         component: () => import("@/views/Doc"),
//         meta: {
//           title: "科室医生查看 （机构）",
//         },
//       },
//       {
//         path: "/home/CurPatientInfo",
//         name: "CurPatientInfo",
//         icon: "iconfont icon-bingli",
//         component: () => import("@/views/CurPatientInfo"),
//         meta: {
//           title: "当前就诊患者 （机构）",
//         },
//       },
//       {
//         path: "/home/PersonMedicalInfo",
//         name: "PersonMedicalInfo",
//         icon: "iconfont icon-bingli",
//         component: () => import("@/views/PersonMedicalInfo"),
//         meta: {
//           title: "患者病历信息",
//         },
//       },
//     ],
//   },
// ];
let role = localStorage.getItem("role");
Vue.use(VueRouter);

let constantRoutes = localStorage.getItem("role") === "1" ? patient : institution;
console.log("===constantRoutes====");
console.log(constantRoutes);
const router = new VueRouter({
  mode: "history",
  routes: constantRoutes,
});

// 全局前置守卫
/* 
  作用;
  判断是否登录，如果未登录，则跳转到登录页面进行登录
  以及页面是否需要超级管理员权限
*/
router.beforeEach((to, from, next) => {
  // //to 目标路由
  // //from 来源
  // //next 放行
  // const userInfo = Cookie.get('userInfo') ? JSON.parse(Cookie.get('userInfo')) : null;
  // console.log(to, from);
  // if (userInfo) {
  //   // 跳转到 home 页面
  //   next("home");
  //   // next();
  // } else


  console.log("===================   ")
  console.log( role === "1")
  if (role === "1") {
    console.log('患者');
    router.options.routes = patient;
  } else if (role !== "1") {
    console.log('机构');
    router.options.routes = institution;
  }

  if (to.name === "login" || to.name === "register") {
    next();
  }
  next();
  // else if (!userInfo) {
  //   console.log('跳转到登录页面');
  //   return next({
  //     name: 'login'
  //   });
  // } else {
  //   // if (to.name === 'admin' && userInfo.is_super !== 1) {
  //   //   next("home");
  //   // }
  //   next();
  // }
});
// http://127.0.0.1:8085/home/approve/admin

// 全局后置守卫：初始化时执行、每次路由切换后执行
router.afterEach((to, from) => {
  // console.log("afterEach");
  // console.log("to: ", to);
  // console.log("from: ", from);
  if (to.meta.title) {
    document.title = to.meta.title; //修改网页的title
  } else {
    document.title = "电子病历系统";
  }
});
export default router;
